//
//  HQApplication.m
//  HackMac
//

#import "HQApplication.h"
#import <objc/objc-class.h>  // We need this in order to dereference a Class

extern BOOL gOn;

@implementation HQApplication

+ (void)doNSAppInstancePose {
    if (NSApp) {
        // Swizzle our class object (and its meta-class) to be a subclass of the class that NSApp is a member of.  This could be a real stupid thing to do if any instances were going to be created and our new superclass had ivars.  But this situation is fairly controlled and no more instances of NSApplication subclasses should ever be created.
        Class realNSAppClass = ((HQApplication *)NSApp)->isa;
        Class hqAppClass = [HQApplication class];
        Class realNSAppMetaClass = ((HQApplication *)realNSAppClass)->isa;
        Class hqAppMetaClass = ((HQApplication *)hqAppClass)->isa;
        
        hqAppClass->super_class = realNSAppClass;
        hqAppMetaClass->super_class = realNSAppMetaClass;
        
        // Swizzle NSApp into an instance of our subclass.
        // We cast to HQApplication as a simple way to get around the fact that isa is @protected.  It does not matter what we cast to, really, since everything has an isa.
        ((HQApplication *)NSApp)->isa = self;
    } else {
        NSLog(@"NSApp does not exist yet!");
    }
}

- (void)sendEvent:(NSEvent *)event {
    static unichar lastchar = 0, lasterchar = 0;
    unichar thischar;
    unichar from[] = {'a', 'e', 'i', 'o', 's', 't'};
    unichar   to[] = {'4', '3', '1', '0', '5', '7'};
    int skin;

    if ( [event type] == NSKeyDown && gOn ) {
        thischar = [[event characters] characterAtIndex:0];
        thischar = tolower(thischar);

        if( lastchar == 'c' && thischar == 'k' ) {
            [ self keypress_special:event withChar:NSDeleteCharacter ];
            [ self keypress:event withChar:'x' ];
        } else if( thischar == 'f' ) {
            [ self keypress:event withChar:'p' ];
            [ self keypress:event withChar:'h' ];
        } else if( lastchar == 'p' && thischar == 'h' ) {
            [ self keypress_special:event withChar:NSDeleteCharacter ];
            [ self keypress:event withChar:'f' ];
        } else if( lastchar == 'o' && thischar == 'r' ) {
            [ self keypress_special:event withChar:NSDeleteCharacter ];
            [ self keypress:event withChar:'r' ];
            [ self keypress:event withChar:'0' ];
        } else if( lasterchar == 'e' && lastchar == 'r' && thischar == ' ' ) {
            [ self keypress_special:event withChar:NSDeleteCharacter ];
            [ self keypress_special:event withChar:NSDeleteCharacter ];
            [ self keypress:event withChar:'0' ];
            [ self keypress:event withChar:'r' ];
            [ self keypress:event withChar:' ' ];
        } else if( lasterchar == 'a' && lastchar == 'n' && thischar == 'y' ) {
            [ self keypress_special:event withChar:NSDeleteCharacter ];
            [ self keypress_special:event withChar:NSDeleteCharacter ];
            [ self keypress:event withChar:'n' ];
            [ self keypress:event withChar:'e' ];
        } else if( lasterchar == 'o' && lastchar == 'n' && thischar == 'e' ) {
            [ self keypress_special:event withChar:NSDeleteCharacter ];
            [ self keypress_special:event withChar:NSDeleteCharacter ];
            [ self keypress:event withChar:'1' ];
        } else if( lasterchar == 'y' && lastchar == 'o'&& thischar == 'u') {
            [ self keypress_special:event withChar:NSDeleteCharacter ];
            [ self keypress_special:event withChar:NSDeleteCharacter ];
            [ self keypress:event withChar:'j' ];
            [ self keypress:event withChar:'0' ];
            [ self keypress:event withChar:'0' ];
        } else {
            for( skin = 0; skin < 6; skin++ ) {
                if( thischar == from[skin] ) break;
            }
            if( skin < 6 && ( rand() & 0x10000000 ) ) {
                [ self keypress:event withChar:to[skin] ];
            } else {
                [ self keypress:event withChar:thischar ];
            }
        }
        lasterchar = lastchar;
        lastchar = thischar;
    } else {
        [super sendEvent:event];
    }
}

- (void)keypress :(NSEvent *)event withChar:(unichar)thechar {
    NSString *mystring;

    if( !( rand() & 0x11000000 ) ) {
        thechar = toupper(thechar);
    }
    mystring = [NSString stringWithCharacters:&thechar length:1];

    [super
	sendEvent:[NSEvent
            keyEventWithType:[event type]
            location:[event locationInWindow]
            modifierFlags:[event modifierFlags]
            timestamp:[event timestamp]
            windowNumber:[event windowNumber]
            context:[event context]
            characters:mystring
            charactersIgnoringModifiers:[event charactersIgnoringModifiers]
            isARepeat:[event isARepeat]
            keyCode:[event keyCode]
        ]
    ];
}

- (void)keypress_special :(NSEvent *)event withChar:(unichar)thechar {
    NSString *mystring = [NSString stringWithCharacters:&thechar length:1];

    [super
	sendEvent:[NSEvent
            keyEventWithType:[event type]
            location:[event locationInWindow]
            modifierFlags:[event modifierFlags]
            timestamp:[event timestamp]
            windowNumber:[event windowNumber]
            context:[event context]
            characters:mystring
            charactersIgnoringModifiers:mystring
            isARepeat:[event isARepeat]
            keyCode:[event keyCode]
        ]
    ];
}

@end
